// pages/me//pages/addMedia/addMedia.js
import { http } from '../../../../utils/apiutils'
import { input } from '../../../../utils/tools'
import { uploadFile } from '../../../../utils/uploadFile'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    name: '',
    detail: '',
    imgfileList: [],
    videofileList: [],
    mediaType: '',//素材类型  0图片  1视频

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.key == 'add') {
      this.setData({
        mediaType: options.mediaType
      })
    } else if (options.key == 'edit') {
      this.setData({
        mediaType: options.mediaType,
        name: options.name,
        detail: options.detail,
        id: options.id - 0,
      })
    }

    if (options.mediaType == 0) {
      let arr = []
      let imgArr = []
      if (options.src) {
        arr = options.src.split(',')
        arr.map(item => {
          imgArr.push({
            url: item,
            type: 'image'
          })
        })
      }
      this.setData({
        imgfileList: imgArr
      })
    } else if (options.mediaType == 1) {
      let arr2 = []
      let imgArr2 = []
      if (options.src) {
        arr2 = options.src.split(',')
        arr2.map(item => {
          imgArr2.push({
            url: item,
            type: 'video'
          })
        })
      }
      this.setData({
        videofileList: imgArr2
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  input(event) {
    input.bind(this)(event)
  },
  afterImgRead({ detail }) {
    let that = this
    if (detail.file.size < 120000000) {
      let params = {
        url: detail.file.url,
        name: 'file',
        formData: {}
      }
      that.upload(params).then(res => {
        if (res.code == 0) {
          detail.file.url = res.data
          if (that.data.mediaType == 0) {
            that.setData({
              imgfileList: [...that.data.imgfileList, detail.file]
            })
          } else if (that.data.mediaType == 1) {
            that.setData({
              videofileList: [...that.data.videofileList, detail.file]
            })
          }

        }
      })
    } else {
      wx.showToast({
        title: '文件过大，请重新选择',
        icon: 'none'
      })
      return
    }
  },
  deleteImg(e) {
    let index = e.detail.index
    if (this.data.mediaType == 0) {
      this.data.imgfileList.splice(index, 1)
      this.setData({
        imgfileList: this.data.imgfileList
      })
    } else if (this.data.mediaType == 1) {
      this.data.videofileList.splice(index, 1)
      this.setData({
        videofileList: this.data.videofileList
      })
    }

  },
  upload(params) {
    return new Promise((rev, rej) => {
      wx.showLoading({
        mask: true
      })
      uploadFile(params).then(res => {
        if (res.code == 0) {
          rev(res)
        }
        wx.hideLoading()
      })
    })
  },
  clickCancel() {
    wx.navigateBack({
      delta: 1,
    })
  },
  clickSubmit() {
    if (this.data.mediaType == 0) {
      this.submitImg()
    } else if (this.data.mediaType == 1) {
      this.submitVideo()
    }
  },
  submitImg() {
    if (!this.data.name || !this.data.detail || this.data.imgfileList.length == 0) {
      wx.showToast({
        title: '标题、简介、图片不能为空',
        icon: 'none'
      })
      return
    }
    let params = {
      type: 1,
      name: this.data.name,
      detail: this.data.detail,
      src: '',
      id: this.data.id ? this.data.id : ''
    }
    let arr = []
    this.data.imgfileList.map(item => {
      arr.push(item.url)
    })
    wx.nextTick(_ => {
      params.src = arr.join(',')
      this.addMaterial(params)
    })
  },
  submitVideo() {
    if (!this.data.name || !this.data.detail || this.data.videofileList.length == 0) {
      wx.showToast({
        title: '标题、简介、视频不能为空',
        icon: 'none'
      })
      return
    }
    let params = {
      type: 0,
      name: this.data.name,
      detail: this.data.detail,
      src: '',
      id: this.data.id ? this.data.id : ''
    }
    let arr = []
    this.data.videofileList.map(item => {
      arr.push(item.url)
    })
    wx.nextTick(_ => {
      params.src = arr.join(',')
      this.addMaterial(params)
    })
  },
  addMaterial(params) {
    http.addMaterial(params, res => {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
      if (res.status == 200) {
        wx.navigateBack({
          delta: 1,
        })
      }
    }, err => { })
  }
})